package com.zpl.common.util;

import com.github.houbb.heaven.util.common.ArgUtil;
import com.zpl.common.rpc.RpcAddress;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/9/8 20:06
 */
public final class InnerAddressUtils {
    
    private InnerAddressUtils() {}
    
    public static List<RpcAddress> initAddress(String address) {
        ArgUtil.notEmpty(address, "address");

        String[] strings = address.split(",");
        List<RpcAddress> list = new ArrayList<>();

        for (String s : strings) {
            String[] infos = s.split(":");

            RpcAddress rpcAddress = new RpcAddress();
            rpcAddress.setAddress(infos[0]);
            rpcAddress.setPort(Integer.parseInt(infos[1]));
            if (strings.length > 2) {
                rpcAddress.setWeight(Integer.parseInt(infos[2]));
            } else {
                rpcAddress.setWeight(1);
            }
            list.add(rpcAddress);
        }
        return list;
    }
    
}
